/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.util.translator.lom2owl.adapter;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;

/**
 *
 * @author elena
 */
/**
 * Adapts the input vCard format to the specific version 3.0 of vCard.
 */
public class vCardAdapter {

    /**
     * This method adapts the vCard entity in to 
     * translate the learning object to OWL.
     *  
     */
    enum CAMPOS {

        BEGIN_VCARD, END_VCARD, FN, N, EMAIL, ORG, VERSION, OTRO
    }

    public String adaptVCard(String file, String dir) throws IOException {

        File directorio =
                new File(dir + System.getProperty("file.separator") + "Results");
        directorio.mkdir();

        String resultedFilePath =
                directorio.getPath() + System.getProperty("file.separator") +
                file.substring(0, file.length() - 4) + "Adapted.xml";

        String filePath =
                dir + System.getProperty("file.separator") + file;

        try {

            BufferedReader bf =
                    new BufferedReader(
                    new InputStreamReader(new FileInputStream(filePath), "UTF8"));

            Writer bw =
                    new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(resultedFilePath), "UTF8"));

            /* Mandatory and optional FIELDS for vCard version 3.0
             * BEGIN:VCARD
             * VERSION:3.0
             * N:
             * FN:
             * [EMAIL:]
             * [ORG:]
             * END:VCARD
             */

            String readString =
                    "";
            CAMPOS campo =
                    null;
            while ((readString =
                    bf.readLine()) != null) {

                //Flags para comprobar que no hay más de un valor de los campos en 
                //cada vCard que existe dentro de un .xml
                boolean n =
                        false;
                boolean fn =
                        false;
                boolean version =
                        false;
                boolean end =
                        false;
                boolean email =
                        false;
                boolean org =
                        false;

                if (readString.contains("BEGIN:VCARD")) {
                    campo =
                            CAMPOS.BEGIN_VCARD;
                } else {
                    campo =
                            CAMPOS.OTRO;
                }

                switch (campo) {
                    case BEGIN_VCARD:

                        bw.write(readString + '\n'); //escribir begin:vcard
                        //System.out.println(readString + '\n');

                        while (((readString =
                                bf.readLine()) != null) && !(end)) {

                            //Asignamos el tipo de datos que son
                            if (readString.contains("VERSION:")) {
                                campo =
                                        CAMPOS.VERSION;
                            } else if (readString.contains("FN:")) {
                                campo =
                                        CAMPOS.FN;
                            } else if (readString.contains("N:")) {
                                campo =
                                        CAMPOS.N;
                            } else if (readString.contains("EMAIL:")) {
                                campo =
                                        CAMPOS.EMAIL;
                            } else if (readString.contains("ORG:")) {
                                campo =
                                        CAMPOS.ORG;
                            } else if (readString.contains("END:VCARD")) {
                                campo =
                                        CAMPOS.END_VCARD;
                            } else {
                                campo =
                                        CAMPOS.OTRO;
                            }

                            //Operaciones según el tipo de dato que sea
                            switch (campo) {

                                case VERSION: //Escribirlo y marcarlo en el flag

                                    if (!version) {
                                        bw.write("VERSION:3.0\n");
                                        //System.out.println("VERSION:3.0\n");
                                        version =
                                                true;
                                    }
                                    break;

                                case FN: //Escribirlo y marcarlo en el flag
                                    if (!fn) {
                                        bw.write(readString + '\n');
                                        //System.out.println(readString + '\n');
                                        fn =
                                                true;
                                    }
                                    break;

                                case N: //Escribirlo y marcarlo en el flag
                                    if (!n) {
                                        bw.write(readString + '\n');
                                        //System.out.println(readString + '\n');
                                        n =
                                                true;
                                    }
                                    break;

                                case EMAIL: //Escribirlo  

                                    if (!email) {
                                        bw.write(readString + '\n');
                                        //System.out.println(readString + '\n');
                                        email =
                                                true;
                                    }
                                    break;
                                case ORG:   //Escribirlo

                                    if (!org) {
                                        bw.write(readString + '\n');
                                        //System.out.println(readString + '\n');
                                        org =
                                                true;
                                    }
                                    break;

                                case END_VCARD:
                                    //Comprobar que los campos 
                                    //obligatorios se han incluido
                                    // y si falta alguno escribirlo sin contenido
                                    if (!fn) {
                                        bw.write("FN:;\n");
                                    //System.out.println("FN:;\n");
                                    }
                                    if (!n) {
                                        bw.write("N:;\n");
                                    //System.out.println("N:;\n");
                                    }
                                    if (!version) {
                                        bw.write("VERSION:3.0\n");
                                    //System.out.println("VERSION:3.0\n");
                                    }
                                    bw.write(readString + '\n');
                                    //System.out.println(readString + '\n');
                                    end =
                                            true;
                                    break;
                                case OTRO:  //puede haber líneas en blanco entre
                                    //medias que hay que eliminar

                                    if (!((readString.compareTo("\n") == 0) ||
                                            ((readString.compareTo("") == 0)))) {
                                        bw.write(readString + '\n');
                                    //System.out.println(readString + '\n');
                                    }
                                    break;
                            }
                        }

                        if (!((readString.compareTo("\n") == 0) ||
                                ((readString.compareTo("") == 0)))) {
                            bw.write(readString + '\n');
                        //System.out.println(readString + '\n');
                        }
                        break;

                    case OTRO:

                        if (!((readString.compareTo("\n") == 0) ||
                                ((readString.compareTo("") == 0)))) {

                            bw.write(readString + '\n');
                            //System.out.println(readString + '\n');
                            break;
                        }
                }
            }
            bw.close();

        } catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        return resultedFilePath;
    }

    public static void main(String args[]) throws IOException {
        vCardAdapter a =
                new vCardAdapter();
        a.adaptVCard("1.xml", "C:/ont-space/Metadatos/");
        a.adaptVCard("2.xml", "C:/ont-space/Metadatos/");
        a.adaptVCard("3.xml", "C:/ont-space/Metadatos/");
        a.adaptVCard("4.xml", "C:/ont-space/Metadatos/");

    }
}
